package descentFly.app.scrollBar
{
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_scrollBar extends uif_ecomponent {
		private var _min	: int;
		private var _max	: int;
		private var _value	: int;
		private var _handle	: df_scrollBarHandle;
		private var _handleLen: Number;
		
		public function df_scrollBar(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, a_styleClass:Object='scrollBar') {
			super(parent, ax, ay, awidth, aheight, a_styleClass);
			_min 	= 0;
			_max 	= 100;
			_value 	= _max;
			_handle = createHandle();
			value = 0;
		}

		// Для переопредления:
		protected function handlePropery1(): String {
			return '';
		}
		
		// Для переопредления:
		protected function handlePropery2(): String {
			return '';
		}
		
		public function get handle(): df_scrollBarHandle {
			return _handle;
		}
		
		protected function getStep(): Number {
			var inner: Rectangle = getInner();
			return (inner[handlePropery2()] - handleLen) / count;
		}
		
		protected function getHandleRect(): Rectangle {
			var inner	: Rectangle = getInner();
			inner[handlePropery2()] = handleLen;
			inner[handlePropery1()] += getStep() * (value - min);	
			return inner;
		}
		
		protected function calcHandleLen(): Number {
			var inner: Rectangle = getInner();
			var mul: Number = 0.9;
			var ah: Number = inner[handlePropery2()] / 10;
			var ch: Number = inner[handlePropery2()] - ah;
			if (count > 3) mul = 3;
			return ch / count * mul + ah;
		}
		
		protected function doHandleDrag(delta: Number): void {
			function round(a_value: Number): int {
				if (a_value > 0) return Math.floor(a_value);
				return Math.ceil(a_value);
			}
			var step: Number = getStep();
			delta = round(delta / step);
			if ((delta != 0)) {
				value += delta;
			};
		}
		
		protected function get handleLen(): Number {
			return _handleLen;
		}
		
		protected function get count(): int {
			return _max - _min;
		}
		
		protected function createHandle(): df_scrollBarHandle {
			var rect: Rectangle = _handleLen?getHandleRect():getInner();
			return new df_scrollBarHandle(this, rect.x, rect.y, rect.width, rect.height, style.handle);
		}
		
		protected function doUpdateValue(): void {
			_handleLen = calcHandleLen();
			_handle.setRect(getHandleRect());
			dispatchEvent(new Event(Event.CHANGE));			
		}
		
		public function get value(): int {
			return _value;
		}

		public function set value(a_value: int): void  {
			setValue(a_value);
		}
		
		public function get min(): int {
			return _min;
		}
		
		public function set min(a_value: int): void  {
			setMin(a_value);
		}
		
		public function get max(): int {
			return _max;
		}
		
		public function set max(a_value: int): void  {
			setMax(a_value);
		}
		
		public function handleDrag(delta: Point): void {
			doHandleDrag(delta[handlePropery1()]);
		}
		
		protected function setValue(a_value: int): void  {
			if (a_value < min) a_value = min;
			else if (a_value > max) a_value = max;
			
			if (_value != a_value) {
				_value = a_value;
				doUpdateValue();
			}
		}
		
		protected function setMin(a_value: int): void  {
			if (_min != a_value) {
				_min = a_value;
				if (_value < _min) value = _min;
				else doUpdateValue();
			}
		}

		protected function setMax(a_value: int): void  {
			if (_max != a_value) {
				_max = a_value;
				if (_value > _max) value = _max;
				else doUpdateValue();
			}
		}		
	}
}